﻿using NAnt.Core.Attributes;
using NAnt.SVN.Attributes;

namespace NAnt.SVN.Tasks
{
    /// <summary>
    /// Implementation of the update command.
    /// You can run this tasc with the svnupdate tag in NAnt.
    /// $Id$
    /// </summary>
    [TaskName("svnupdate")]
    sealed public class Update : SVNTask
    {
        #region Private Fields

        string accept = string.Empty;
        bool force = false;

        #endregion

        #region Public fields

        /// <summary>
        /// Attribute value to check if theirs schould be used
        /// Used in the Accept attribute
        /// </summary>
        public static string ACCEPTTHEIRSATTRVALUE = "theirs";
        
        /// <summary>
        /// Attribute value to check if mine should be used
        /// Used in the Accept attribute
        /// </summary>
        public static string ACCEPTMINEATTRVALUE = "mine";

        /// <summary>
        /// Accept value for automatic conflict resolving. Currently
        /// accepting 'theirs' and 'mine' values coresponding to
        /// theirs-full and mine-full.
        /// </summary>
        [TaskAttribute("accept",Required=false)]
        [AcceptAttributeValidator()]
        public string Accept 
        {
            get { return accept; }
            set { accept = value; }
        }

        /// <summary>
        /// Force it or not.
        /// </summary>
        [TaskAttribute("force",Required=false)]
        [BooleanValidator()]
        public bool Force
        {
            get { return force; }
            set { force = value; }
        }

        #endregion

        /// <summary>
        /// Initialize command
        /// </summary>
        protected override void InitializeSVNTask()
        {
            Command = SVNCommands.UPDATE;
            if (Force)
            {
                Options.Add(SVNFlags.FORCE);
            }
            if (!string.IsNullOrEmpty(Accept))
            {
                if (Accept.Equals(ACCEPTMINEATTRVALUE)) 
                    Options.Add(SVNFlags.ACCEPTMINEFULL);
                if (accept.Equals(ACCEPTTHEIRSATTRVALUE)) 
                    Options.Add(SVNFlags.ACCEPTTHEIRSFULL);
            }
        }
    }
}